$(function () {
    //获取文章类别,进行动态渲染
    $.ajax({
        url: News.category_list,
        headers: { Authorization: localStorage.getItem('get_big_token') },
        success: function (res) {
            let html = template('itemizeTemp', res)
            $('.classId').html(html)
        }
    })

    /* 2.js初始化选项 */
    tinymce.init({
        selector: '#itemcontent',
        height: '350px',
        language: 'zh_CN',
        directionality: 'ltl',
        browser_spellcheck: true,
        contextmenu: false,
        plugins: [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table contextmenu paste imagetools wordcount",
            "code"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | code",
    });

    //时间插件蓝色主题色
    jeDate("#postdate", {
        theme: { bgcolor: "#00A1CB", color: "#ffffff", pnColor: "#00CCFF" },
        format: "YYYY-MM-DD"
    });

    //获取编辑的内容填充
    let data = location.href
    let Idlength = data.lastIndexOf('=')
    let id = data.slice(Idlength + 1)
    $.ajax({
        url: News.article_search,
        headers: { Authorization: localStorage.getItem('get_big_token') },
        data: { id },
        success: function (res) {
            let dataId = res.data
            $('#inputTitle').val(dataId.title)
            $('.col-sm-10').find('img').prop('src', dataId.cover)
            $('#postdate').val(dataId.date)
            $('#categoryId').prop('value', dataId.categoryId)
            setTimeout(function () {
                tinymce.activeEditor.setContent(dataId.content)
            }, 100)
        }
    })


    //上传图片的改变事件处理
    $('#inputCover').on('change', function () {
        if ($('#inputCover')[0].files[0].type.indexOf('image') == -1) {
            alert('请上传图片作为封面')
            return
        }
        let file = $(this)[0].files[0]
        let url = URL.createObjectURL(file);
        $('.article_cover').attr('src', url);
    })

    //修改按钮的点击事件
    $('.btnItems button').on('click', function () {
        if ($(this).text() == '修改') {
            btnItem('已发布')
        } else {
            btnItem('草稿')
        }
    })


    //将修改按钮和存为草稿的事件处理封装成函数重复调用
    var btnItem = function (state) {
        let formdata = new FormData($('#form')[0])
        let content = tinymce.activeEditor.getContent()
        if (content.length == 0) {
            alert('请输入文章内容')
            return
        }
        formdata.set('content', content)
        formdata.append('id', id)
        formdata.append('state', state)
        $.ajax({
            type: 'post',
            url: News.article_edit,
            headers: { Authorization: localStorage.getItem('get_big_token') },
            data: formdata,
            processData: false,
            contentType: false,
            success: function (res) {
                alert(res.msg)
                window.history.go(-1)
            },
            error: function (err) {
                alert(err.msg)
            },
            dataType: 'json'
        })
    }
})


